package com.teamwork.ZuccEcho.Controller;
//
//import com.teamwork.ZuccEcho.Entity.Model;
//import com.teamwork.ZuccEcho.Entity.Question;
//import com.teamwork.ZuccEcho.Repository.ModelApiRepository;

import com.alibaba.fastjson.JSON;
import com.teamwork.ZuccEcho.Entity.Model;
import com.teamwork.ZuccEcho.Repository.ModelApiRepository;
import com.teamwork.ZuccEcho.Repository.ModelQuestionApiRepository;
import com.teamwork.ZuccEcho.Result.ExceptionMsg;
import com.teamwork.ZuccEcho.Result.Response;
import com.teamwork.ZuccEcho.Result.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/model")
public class ModelApiController {
    protected Response result(ExceptionMsg msg){
        return new Response(msg);
    }
    protected Response result(){
        return new Response();
    }

    @Autowired
    private ModelApiRepository modelApiRepository;
    @Autowired
    private ModelQuestionApiRepository modelquestionApiRepository;
    @Autowired
    RestTemplateBuilder restTemplateBuilder;

    public List<Model> models;//所有模板

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseData getModelList() {
        List<Model> list = new ArrayList<Model>(modelApiRepository.findAll());
        return new ResponseData(ExceptionMsg.SUCCESS,list);
    }

    //添加模板
    @RequestMapping(value = "/add/v1/", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData addModel(@RequestBody Model model){
//        Model model = new Model();
//        model.setM_name(m_name);
//        model.setM_illustration(m_illustration);

        modelApiRepository.save(model);
        return new ResponseData(ExceptionMsg.SUCCESS,model);
    }

    //删除模板
    @RequestMapping(value = "/delete/v1/{id}", method = RequestMethod.POST)
    public Response deleteModel(@PathVariable("id") long id){
        List<Long> questionsid=modelquestionApiRepository.searchAllModelQuestionsByModelId(id);

        //删除模板问题表(model_question)的记录
        for(int i=0;i<questionsid.size();i++){
            //删除模板问题的选项
            List<Long> optionsid=modelquestionApiRepository.searchAllModelOptionsByQuestionId(questionsid.get(i));
            for(int j=0;j<optionsid.size();j++){
                modelquestionApiRepository.deleteOptionByOId(optionsid.get(j));
            }

            modelquestionApiRepository.deleteById(questionsid.get(i));
        }

        //删除模板表的记录
        modelApiRepository.deleteById(id);

        return result(ExceptionMsg.SUCCESS);
    };

    //修改模板
    @RequestMapping(value = "/put/v1/{id}", method = RequestMethod.POST)
    public ResponseData updateModel(@PathVariable("id") long id, @RequestBody Model model){
        modelApiRepository.modifyModelByMId(model.getM_name(),model.getM_illustration(),id);
        return new ResponseData(ExceptionMsg.SUCCESS,model);
    };

    //查询模板(根据模板ID)
    @RequestMapping(value = "/get/v1/{id}", method = RequestMethod.GET)
    public ResponseData findModel(@PathVariable("id") long id){
        List<Model> list = new ArrayList<>(modelApiRepository.findModelByModelId(id));
        String strJSON = JSON.toJSONString(list.get(0).toString());//将要返回的列表转为json字符串，返回给前端
        return new ResponseData(ExceptionMsg.SUCCESS,strJSON);
//        Model model = modelApiRepository.findById(id);
//        if (model != null) {
//            return new ResponseData(ExceptionMsg.SUCCESS,model);
//        }
//        return new ResponseData(ExceptionMsg.FAILED,model);
    };


}